package zhang.automobileservice;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;

import org.json.JSONException;
import org.json.JSONObject;

import bean.Second_Car_Item;
import utils.MyStoreDao;
import utils.MyUrl;
import utils.RequestQueueUtils;

public class SecondCarDetialActivity extends FarterActivity implements View.OnClickListener {

    private NetworkImageView image_title;
    private TextView tv_rank;
    private LinearLayout LL_collect;
    private LinearLayout LL_see;
    private LinearLayout LL_tel;
    private LinearLayout ll_bottom;
    private TextView tv_title;
    private TextView tv_price;
    private TextView tv_moremessage;
    private TextView tv_km;
    private TextView tv_05;
    private TextView tv_time;
    private TextView tv_auto;
    private TextView tv_bodycolor;
    private TextView tv_num;
    private TextView tv_address;
    Second_Car_Item secondCar = new Second_Car_Item();
    private TextView tv_tab;
    private TextView tv_company;
    private TextView tv_adress;
    private TextView tv_details;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_car_detial);
        initView();
        initData();
    }

    private void initData() {
        String url = MyUrl.Font_Url + MyUrl.queryById;
        RequestQueueUtils.exectueJsonRequest(this, url + getIntent().getStringExtra("id"), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    if ("0".equals(o.getString("code"))) {
                        JSONObject obj = o.getJSONObject("data");
                        secondCar.setId(obj.getInt("id") + "");
                        secondCar.setType(obj.getString("type"));
                        secondCar.setTitle(obj.getString("title"));
                        secondCar.setPrice(obj.getDouble("price") + "");
                        secondCar.setKm(obj.getString("km"));
                        secondCar.setCompany(obj.getString("company"));
                        secondCar.setPlace(obj.getString("place"));
                        secondCar.setDetail(obj.getString("detail"));
                        secondCar.setBuyDate(obj.getString("buyDate"));
                        secondCar.setAuto(obj.getString("auto"));
                        secondCar.setOutputValue(obj.getDouble("outputValue") + "");
                        secondCar.setColor(obj.getString("color"));
                        secondCar.setTransferTime(obj.getInt("transferTime") + "");
                        secondCar.setDistrict(obj.getString("district"));
                        secondCar.setFilm(obj.getString("film"));
                        secondCar.setLevel(obj.getString("level"));
                        secondCar.setEngine(obj.getString("engine"));
                        secondCar.setGearbox(obj.getString("gearbox"));
                        secondCar.setStructre(obj.getString("structre"));
                        secondCar.setVolume(obj.getString("volume"));
                        secondCar.setWheelbase(obj.getInt("wheelbase") + "");
                        secondCar.setTrunksize(obj.getInt("trunksize") + "");
                        secondCar.setQuality(obj.getInt("quality") + "");
                        secondCar.setImage(obj.getString("image"));
                        secondCar.setTab(obj.getString("tab"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                setView();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_moremessage:
                Intent intent = new Intent(this,MoreCarDetialActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("bundle",secondCar);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.LL_collect:
                MyStoreDao myStoreDao = new MyStoreDao(this);
                myStoreDao.insert(getIntent().getStringExtra("id"));
                break;
            case R.id.LL_see:
                Intent intent1 = new Intent(this,PresetWatchCarActivity.class);
                startActivity(intent1);
                break;
            case R.id.LL_tel:
                String tel = secondCar.getDetail().split("：")[1].trim();
                Intent intent2=new Intent();
                intent2.setAction("android.intent.action.CALL");//指定动作名称，这个只在上面的Activity中找 Intent.ACTION_CALL;
                intent2.setData(Uri.parse("tel:"+tel));//传递号码，tel:实在上面Activity中定义的
                startActivity(intent2);
                break;
        }
    }

    private void initView() {
        image_title = (NetworkImageView) findViewById(R.id.image_title);
        tv_rank = (TextView) findViewById(R.id.tv_rank);
        LL_collect = (LinearLayout) findViewById(R.id.LL_collect);
        LL_collect.setOnClickListener(this);
        LL_see = (LinearLayout) findViewById(R.id.LL_see);
        LL_see.setOnClickListener(this);
        LL_tel = (LinearLayout) findViewById(R.id.LL_tel);
        LL_tel.setOnClickListener(this);
        ll_bottom = (LinearLayout) findViewById(R.id.ll_bottom);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_price = (TextView) findViewById(R.id.tv_price);
        tv_moremessage = (TextView) findViewById(R.id.tv_moremessage);
        tv_moremessage.setOnClickListener(this);
        tv_km = (TextView) findViewById(R.id.tv_km);
        tv_05 = (TextView) findViewById(R.id.tv_05);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_auto = (TextView) findViewById(R.id.tv_auto);
        tv_bodycolor = (TextView) findViewById(R.id.tv_bodycolor);
        tv_num = (TextView) findViewById(R.id.tv_num);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_tab = (TextView) findViewById(R.id.tv_tab);
        tv_tab.setOnClickListener(this);
        tv_company = (TextView) findViewById(R.id.tv_company);
        tv_company.setOnClickListener(this);
        tv_adress = (TextView) findViewById(R.id.tv_adress);
        tv_adress.setOnClickListener(this);
        tv_details = (TextView) findViewById(R.id.tv_details);
        tv_details.setOnClickListener(this);
    }

    private void setView() {
        String[] arr = secondCar.getImage().split(",");
        String url = MyUrl.Font_Url + MyUrl.CarImage;
        RequestQueueUtils.exectueNetWorkImageView(this, url + arr[0], image_title);
        Log.e("----",url +arr[0]);
        Log.e("----",url +arr[1]);
        Log.e("----",url +arr[2]);
        Log.e("----",url +arr[3]);
        tv_title.setText(secondCar.getTitle());
        tv_price.setText(secondCar.getPrice() + "万");
        tv_tab.setText(secondCar.getTab());
        tv_km.setText(secondCar.getKm() + "万公里");
        tv_time.setText(secondCar.getBuyDate());
        tv_auto.setText(secondCar.getAuto() + "/" + secondCar.getEngine());
        tv_bodycolor.setText(secondCar.getColor());
        tv_num.setText(secondCar.getTransferTime().toString());
        tv_address.setText(secondCar.getDistrict());
        tv_company.setText(secondCar.getCompany());
        tv_adress.setText(secondCar.getPlace());
        tv_details.setText(secondCar.getDetail());
    }
}
